package com.yh.correct.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yh.common.utils.ExcelUtil;
import com.yh.correct.dao.IGuardianMapper;
import com.yh.correct.model.Guardian;
import com.yh.correct.model.Page;
import com.yh.correct.service.IGuardianService;
/**
 * 监护人 业务层 实现类
 * @author Administrator
 */
@Service
public class GuardianServiceImpl implements IGuardianService {

	private static Logger logger = LogManager.getLogger(GuardianServiceImpl.class);
		
	@Autowired
	private IGuardianMapper guardianMapper;
	/**
	 * 新增监护人	
	 */
	@Override
	public int addGuardian(Guardian gd) {
		logger.debug("新增监护人"+gd);
		return guardianMapper.addGuardian(gd);
	}
	/**
	 * 根据id删除监护人
	 */
	@Override
	public int delGuardian(Guardian gd) {
		logger.debug("根据id删除监护人"+gd);
		return guardianMapper.delGuardian(gd);
	}
	/**
	 * 根据id修改监护人
	 */
	@Override
	public int updateGuardian(Guardian gd) {
		logger.debug("根据id修改监护人"+gd);
		return guardianMapper.updateGuardian(gd);
	}
	/**
	 * 根据id查询监护人
	 */
	@Override
	public Map<String,Object> getGuardian(Guardian gd) {
		logger.debug("根据id查询监护人"+gd);
		return guardianMapper.getGuardian(gd);
	}
	/**
	 * 模糊查询匹配的监护人
	 */
	@Override
	public Page queryLikeGuardians(Map<String, Object> cond) {
		logger.debug("模糊查询匹配的监护人"+cond);
		Page page=new Page();
		if(cond.get("pageSize")==null){
			List<Object> list=guardianMapper.getLikeGuardians(cond);
			page.setResult(list);
		}else{
			//根据条件查询符合的用户列表记录总数，赋值给page的totalNum变量
			page.setTotalNum(guardianMapper.getLikeGuardiansCount(cond));
			//从请求参数中获取每页大小
			int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
			page.setPageSize(pageSize);
			//从请求参数中获取当前页码
			int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
			page.setCurPageNum(curPageNum);
			//动态计算总页数(总记录数 除以 每页大小，加上  总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
			page.setTotalPage(page.getTotalNum()/pageSize+(page.getTotalNum()%pageSize==0?0:1));
			//根据条件查询符合的用户列表记录，赋值给page的result变量
			page.setResult(guardianMapper.getLikeGuardians(cond));
		}
			return page;
	}
	
	/**
	 * 根据矫正人员查询其司法所或根据司法所查询所有矫正人员
	 */
	@Override
	public List<Object> getCorrectedPersonAll(Map<String, Object> cond) {
		logger.debug("根据矫正人员查询其司法所或根据司法所查询所有矫正人员"+cond);
		return guardianMapper.getCorrectedPersonAll(cond);
	}
	
	/**
	 * 导出匹配的监护人员
	 */
	@Override
	public Workbook exportGuardians(Map<String, Object> cond) {
		logger.debug("导出匹配的监护人员"+cond);
		List<Object> list=guardianMapper.getLikeGuardians(cond);
		List<Map<String, Object>> list2=new ArrayList<Map<String, Object>>();
		Map<String,Object> sheetMap=new HashMap<String,Object>();
		sheetMap.put("sheetName","监护人员");
		list2.add(sheetMap);
		for(Object obj:list){
			Map<String,Object> map=(Map<String,Object>)obj;
			list2.add(map);
		}
		String[] columnNames={"所属单位","姓名","性别","出生日期","证件号码","职业","状态","帮教对象"}; 
		String[] keys={"jdName","gName","sex","birthday","card","profession","status","cpName"}; 
		Workbook wb=ExcelUtil.createWorkBook(list2, keys, columnNames);
		return wb;
	}

}
